home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gsdsrc.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  3.2 KB  |  114 lines

  1. /* Copyright (C) 1997, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gsdsrc.c,v 1.2 2000/09/19 19:00:28 lpd Exp $ */
  20. /* DataSource procedures */
  21.  
  22. #include "memory_.h"
  23. #include "gx.h"
  24. #include "gsdsrc.h"
  25. #include "gserrors.h"
  26. #include "stream.h"
  27.  
  28. /* GC descriptor */
  29. public_st_data_source();
  30. private 
  31. ENUM_PTRS_WITH(data_source_enum_ptrs, gs_data_source_t *psrc)
  32. {
  33.     if (psrc->type == data_source_type_string)
  34.     ENUM_RETURN_CONST_STRING_PTR(gs_data_source_t, data.str);
  35.     else if (psrc->type == data_source_type_stream)
  36.     ENUM_RETURN_PTR(gs_data_source_t, data.strm);
  37.     else            /* bytes or floats */
  38.     ENUM_RETURN_PTR(gs_data_source_t, data.str.data);
  39. }
  40. ENUM_PTRS_END
  41. private RELOC_PTRS_WITH(data_source_reloc_ptrs, gs_data_source_t *psrc)
  42. {
  43.     if (psrc->type == data_source_type_string)
  44.     RELOC_CONST_STRING_PTR(gs_data_source_t, data.str);
  45.     else if (psrc->type == data_source_type_stream)
  46.     RELOC_PTR(gs_data_source_t, data.strm);
  47.     else            /* bytes or floats */
  48.     RELOC_PTR(gs_data_source_t, data.str.data);
  49. }
  50. RELOC_PTRS_END
  51.  
  52. /* Access data from a string or a byte object. */
  53. /* Does *not* check bounds. */
  54. int
  55. data_source_access_string(const gs_data_source_t * psrc, ulong start,
  56.               uint length, byte * buf, const byte ** ptr)
  57. {
  58.     const byte *p = psrc->data.str.data + start;
  59.  
  60.     if (ptr)
  61.     *ptr = p;
  62.     else
  63.     memcpy(buf, p, length);
  64.     return 0;
  65. }
  66. /* access_bytes is identical to access_string, but has a different */
  67. /* GC procedure. */
  68. int
  69. data_source_access_bytes(const gs_data_source_t * psrc, ulong start,
  70.              uint length, byte * buf, const byte ** ptr)
  71. {
  72.     const byte *p = psrc->data.str.data + start;
  73.  
  74.     if (ptr)
  75.     *ptr = p;
  76.     else
  77.     memcpy(buf, p, length);
  78.     return 0;
  79. }
  80.  
  81. /* Access data from a stream. */
  82. /* Returns gs_error_rangecheck if out of bounds. */
  83. int
  84. data_source_access_stream(const gs_data_source_t * psrc, ulong start,
  85.               uint length, byte * buf, const byte ** ptr)
  86. {
  87.     stream *s = psrc->data.strm;
  88.     const byte *p;
  89.  
  90.     if (start >= s->position &&
  91.     (p = start - s->position + s->cbuf) + length <=
  92.     s->cursor.r.limit + 1
  93.     ) {
  94.     if (ptr)
  95.         *ptr = p;
  96.     else
  97.         memcpy(buf, p, length);
  98.     } else {
  99.     uint nread;
  100.     int code = sseek(s, start);
  101.  
  102.     if (code < 0)
  103.         return_error(gs_error_rangecheck);
  104.     code = sgets(s, buf, length, &nread);
  105.     if (code < 0)
  106.         return_error(gs_error_rangecheck);
  107.     if (nread != length)
  108.         return_error(gs_error_rangecheck);
  109.     if (ptr)
  110.         *ptr = buf;
  111.     }
  112.     return 0;
  113. }
  114.